import turtle
import random
import time

# 烟花颜色列表，可以根据喜好添加更多颜色
colors = ["red", "orange", "yellow", "green", "blue", "purple"]


# 粒子类
class Particle(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.penup()
        self.speed(0)
        self.size = random.randint(2, 6)
        self.color(random.choice(colors))
        self.shapesize(self.size / 10)
        self.goto(random.randint(-300, 300), random.randint(-200, 200))
        self.direction = random.randint(0, 360)
        self.speed_value = random.randint(2, 6)

    def move(self):  # 添加了缺失的 move 方法
        self.forward(self.speed_value)
        self.direction += random.randint(-20, 20)
        self.setheading(self.direction)


# 烟花类
class Firework:
    def __init__(self):
        self.particles = []
        x = random.randint(-300, 300)
        y = random.randint(-200, 200)
        self.firework = turtle.Turtle()
        self.firework.shape("circle")
        self.firework.penup()
        self.firework.speed(0)
        self.firework.goto(x, y)
        self.firework.color(random.choice(colors))
        self.firework.shapesize(3)

    def explode(self):
        for _ in range(30):
            particle = Particle()
            particle.goto(self.firework.xcor(), self.firework.ycor())
            self.particles.append(particle)

    def update(self):
        for particle in self.particles:
            particle.move()


# 创建画布
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("粒子烟花")
screen.setup(width=800, height=600)
screen.tracer(0)

# 存储烟花对象的列表
fireworks = []

while True:
    # 随机概率生成新烟花
    if random.random() < 0.1:
        firework = Firework()
        firework.explode()
        fireworks.append(firework)

    for firework in fireworks:
        firework.update()

    screen.update()
    time.sleep(0.05)

turtle.done()
